class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        //层序遍历每一层的最后一个节点
        vector<int> ret;
        queue<TreeNode*> q;
        int levelsize = 0;
        if(root)
        {
            q.push(root);
            levelsize=1;
        }
        while (!q.empty()) {
            while (levelsize--) {
                TreeNode* cur = q.front();
                q.pop();
                if (levelsize == 0) {
                    ret.push_back(cur->val);
                }
                if (cur->left) {
                    q.push(cur->left);
                }
                if (cur->right) {
                    q.push(cur->right);
                }
            }
            levelsize=q.size();
        }
        return ret;
    }
};